<?php

/**
 * Description of PersonDao_Class
 *
 * @author aziz
 */
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Team_Class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Country_Class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Person_Class.php';
require_once 'GenericDAO_Class.php';
require_once 'TeamDAO_Class.php';
require_once 'CountryDAO_Class.php';

class PersonDAO extends GenericDAO {
    
    public $teamDAO;
    public $countryDAO;

    public function __construct() {
        $this->teamDAO=new Team();
        $this->countryDAO=new CountryDAO();
    }

    public function prepareService($module, $id, $object) {
        $this->type = PERSON_SERVICE_MOD;
        $this->module = $module;
        $this->id = $id;
        $this->object = $object;
        $this->__setUp();
    }

    public function loadAll() {
        $this->prepareService(SERVICE_ALL, null, null);
        $personeller = $this->getRequest();
        for ($i = 0; $i < count($personeller); $i++) {
            $person = new Person();
            $person->setId($personeller[$i]->id);
            $person->setName($personeller[$i]->name);
            $person->setPosition($personeller[$i]->position);
            $person->setBirthday($personeller[$i]->birthday);
            $person->setContratStartDate($personeller[$i]->contratStartDate);
            $person->setContratFinishDate($personeller[$i]->contratFinishDate);
            $person->setTeam($this->teamDao->loadById($personeller[$i]->team->id));
            $person->setType($personeller[$i]->type);
            $person->setNationality($this->countryDao->loadById($personeller[$i]->nationality->id));
            $persons[] = $person;
        }
        return $persons;
    }

    public function loadById($id) {
        $this->prepareService(SERVICE_SINGLE, $id, null);
        $_person = $this->getRequest();
        $person = new Person();
        $person->setId($_person->id);
        $person->setName($_person->name);
        $person->setPosition($_person->position);
        $person->setBirthday($_person->birthday);
        $person->setContratStartDate($_person->contratStartDate);
        $person->setContratFinishDate($_person->contratFinishDate);
        $person->setTeam($this->teamDao->loadById($_person->team->id));
        $person->setType($_person->type);
        $person->setNationality($this->countryDao->loadById($_person->nationality->id));
  
        return $person;
    }

    public function delete($id) {
        $this->prepareService(SERVICE_DELETE, $id, null);
        $this->deleteRequest();
    }

    public function upsert($person) {
        $this->prepareService(SERVICE_SAVE, null, $person);
        $this->postRequest();
    }

}

?>
